﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;
using PurchaseOrderExtra;

namespace BusinessLogicLayer
{
    public class SelectItemsForPurchasingControl
    {    
        UserRepository userRepo;
        CategoryRepository categoryRepo;
        StationeryRepository stationeryRepo;
        SupplierDetailRepository supplierDetailRepo;
        TempPOItemRepository tempPOItemRepo;
        EmployeeRepository employeeRepo;

        public SelectItemsForPurchasingControl()
        {
            userRepo = new UserRepository();
            categoryRepo = new CategoryRepository();
            stationeryRepo = new StationeryRepository();
            supplierDetailRepo = new SupplierDetailRepository();
            tempPOItemRepo = new TempPOItemRepository();
            employeeRepo = new EmployeeRepository();
        }

        public User GetUserByUsername(string username)
        {
            return userRepo.GetUserByUsername(username);
        }

        public Employee GetEmployeeByUserId(int userId)
        {
            return employeeRepo.GetEmployeeByUserId(userId);
        }

        public List<Category> DisplayAllCategory()
        {
            List<Category> list = new List<Category>();
            // Add all
            Category category = new Category();
            category.CategoryID = 0;
            category.CategoryName = "All";
            list.Add(category);
            // Add categories
            List<Category> categoryList = categoryRepo.GetAllCategory();
            foreach (Category cat in categoryList)
            {
                list.Add(cat);
            }
            return list;
        }

        public List<StationeryDisplay> DisplayAllStationery()
        {
            List<StationeryDisplay> list = new List<StationeryDisplay>();
            List<Stationery> statList = stationeryRepo.GetAllStationery();
            foreach (Stationery s in statList)
            {
                StationeryDisplay statDisplay = new StationeryDisplay();
                statDisplay.ItemCode = s.ItemCode;
                statDisplay.ItemName = s.ItemName;
                statDisplay.Category = s.Category.CategoryName;
                statDisplay.ReorderLevel = s.ReorderLevel;
                statDisplay.ReorderQuantity = s.ReorderLevel;
                statDisplay.UnitOfMeasurement = s.UOM;
                statDisplay.AvailableQuantity = s.AvailableQty;
                list.Add(statDisplay);
            }
            return list;
        }

        public List<StationeryDisplay> DisplayStationeryByCategory(int categoryId)
        {
            List<StationeryDisplay> list = new List<StationeryDisplay>();
            List<Stationery> statList = stationeryRepo.GetStationeryByCategoryId(categoryId);
            foreach (Stationery s in statList)
            {
                StationeryDisplay statDisplay = new StationeryDisplay();
                statDisplay.ItemCode = s.ItemCode;
                statDisplay.ItemName = s.ItemName;
                statDisplay.Category = s.Category.CategoryName;
                statDisplay.ReorderLevel = s.ReorderLevel;
                statDisplay.ReorderQuantity = s.ReorderLevel;
                statDisplay.UnitOfMeasurement = s.UOM;
                statDisplay.AvailableQuantity = s.AvailableQty;
                list.Add(statDisplay);
            }
            return list;
        }

        public List<StationeryDisplay> DisplayAllLowStockStationery()
        {
            List<StationeryDisplay> list = new List<StationeryDisplay>();
            List<Stationery> statList = stationeryRepo.GetAllLowStockStationery();
            foreach (Stationery s in statList)
            {
                StationeryDisplay statDisplay = new StationeryDisplay();
                statDisplay.ItemCode = s.ItemCode;
                statDisplay.ItemName = s.ItemName;
                statDisplay.Category = s.Category.CategoryName;
                statDisplay.ReorderLevel = s.ReorderLevel;
                statDisplay.ReorderQuantity = s.ReorderLevel;
                statDisplay.UnitOfMeasurement = s.UOM;
                statDisplay.AvailableQuantity = s.AvailableQty;
                list.Add(statDisplay);
            }
            return list;
        }

        public List<StationeryDisplay> DisplayLowStockStationeryByCategory(int categoryId)
        {
            List<StationeryDisplay> list = new List<StationeryDisplay>();
            List<Stationery> statList = stationeryRepo.GetLowStockStationeryByCategoryId(categoryId);
            foreach (Stationery s in statList)
            {
                StationeryDisplay statDisplay = new StationeryDisplay();
                statDisplay.ItemCode = s.ItemCode;
                statDisplay.ItemName = s.ItemName;
                statDisplay.Category = s.Category.CategoryName;
                statDisplay.ReorderLevel = s.ReorderLevel;
                statDisplay.ReorderQuantity = s.ReorderLevel;
                statDisplay.UnitOfMeasurement = s.UOM;
                statDisplay.AvailableQuantity = s.AvailableQty;
                list.Add(statDisplay);
            }
            return list;
        }

        public List<SupplierDetailDisplay> DisplaySupplierDetail(string itemCode)
        {
            List<SupplierDetailDisplay> list = new List<SupplierDetailDisplay>();
            List<SupplierDetail> supDetailList = supplierDetailRepo.GetSupplierDetailByItemCode(itemCode);
            foreach (SupplierDetail sd in supDetailList)
            {
                SupplierDetailDisplay sdDisplay = new SupplierDetailDisplay();
                sdDisplay.ItemName = sd.Stationery.ItemName;
                sdDisplay.ItemCode = itemCode;
                sdDisplay.Rank = (int)sd.Rank;
                sdDisplay.SupplierId = sd.SupplierID;
                sdDisplay.SupplierName = sd.Supplier.SupplierName;
                sdDisplay.SupplierDetailId = sd.SupplierDetailID;
                sdDisplay.ContactNo = sd.Supplier.PhoneNo;
                sdDisplay.Price = (decimal)sd.Price;
                list.Add(sdDisplay);
            }
            return list;
        }

        public void SaveTempPOItem(TempPOItem newTempPOItem)
        {
            //try
            //{
                TempPOItem tempPOItem = tempPOItemRepo.SelectTempPOItemByNewTempPOItem(newTempPOItem);
                if (tempPOItem != null)
                {
                    newTempPOItem.TempPOID = tempPOItem.TempPOID;
                    newTempPOItem.Quantity = tempPOItem.Quantity + 1;
                    newTempPOItem.Date = DateTime.Now;
                    tempPOItemRepo.UpdateTempPOItem(newTempPOItem);
                }
                else
                {
                    tempPOItemRepo.CreateTempPOItem(newTempPOItem);
                }
            //}
            //catch (Exception ex)
            //{
            //}
        }

        public string RedirectToRaisePurchaseOrder()
        {
            string redirectURL = "/RaisePurchaseOrderList.aspx";
            return redirectURL;
        }
    }
}
